home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / axbutton / clspnttl.cls < prev    next >
Encoding:
Visual Basic class definition  |  1999-02-02  |  55.1 KB  |  1,287 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "PaintEffects"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = False
  10. Attribute VB_Description = "Provides methods for painting transparent and disabled looking images."
  11. '*****************************************************************
  12. '
  13. '   POPUPCOMMAND CONTROL
  14. '
  15. '   This code and control is absolutely freeware!
  16. '
  17. '   You have a royalty-free right to use, modify, reproduce and distribute
  18. '   the source code and control (and/or any modified version) in any way
  19. '   you find useful, provided that you agree that the authors have no warranty,
  20. '   obligations or liability for any code distributed in this project group.
  21. '
  22. ' Copyright ⌐ 1998 by Geoff Glaze
  23. '
  24. '   (Some parts borrowed from Microsoft)
  25. '
  26. '*****************************************************************
  27.  
  28.  
  29. '-------------------------------------------------------------------------
  30. 'This class provides methods needed for painting masked bitmaps and
  31. 'disabled or embossed bitmaps and icons
  32. '-------------------------------------------------------------------------
  33.  
  34. Option Explicit
  35.  
  36. Private m_hpalHalftone As Long  'Halftone created for default palette use
  37.  
  38. '-------------------------------------------------------------------------
  39. 'Purpose:   Creates a disabled-appearing (grayed) bitmap, given any format of
  40. '           input bitmap.
  41. 'In:
  42. '   [hdcDest]
  43. '           Device context to paint the picture on
  44. '   [xDest]
  45. '           X coordinate of the upper left corner of the area that the
  46. '           picture is to be painted on. (in pixels)
  47. '   [yDest]
  48. '           Y coordinate of the upper left corner of the area that the
  49. '           picture is to be painted on. (in pixels)
  50. '   [Width]
  51. '           Width of picture area to paint in pixels.  Note: If this value
  52. '           is outrageous (i.e.: you passed a forms ScaleWidth in twips
  53. '           instead of the pictures' width in pixels), this procedure will
  54. '           attempt to create bitmaps that require outrageous
  55. '           amounts of memory.
  56. '   [Height]
  57. '           Height of picture area to paint in pixels.  Note: If this
  58. '           value is outrageous (i.e.: you passed a forms ScaleHeight in
  59. '           twips instead of the pictures' height in pixels), this
  60. '           procedure will attempt to create bitmaps that require
  61. '           outrageous amounts of memory.
  62. '   [picSource]
  63. '           Standard Picture object to be used as the image source
  64. '   [xSrc]
  65. '           X coordinate of the upper left corner of the area in the picture
  66. '           to use as the source. (in pixels)
  67. '           Ignored if picSource is an Icon.
  68. '   [ySrc]
  69. '           Y coordinate of the upper left corner of the area in the picture
  70. '           to use as the source. (in pixels)
  71. '           Ignored if picSource is an Icon.
  72. '   [clrMask]
  73. '           Color of pixels to be masked out
  74. '   [clrHighlight]
  75. '           Color to be used as outline highlight
  76. '   [clrShadow]
  77. '           Color to be used as outline shadow
  78. '   [hPal]
  79. '           Handle of palette to select into the memory DC's used to create
  80. '           the painting effect.
  81. '           If not provided, a HalfTone palette is used.
  82. '-------------------------------------------------------------------------
  83. Public Sub PaintDisabledStdPic(ByVal hdcDest As Long, _
  84.                                 ByVal xDest As Long, _
  85.                                 ByVal yDest As Long, _
  86.                                 ByVal Width As Long, _
  87.                                 ByVal Height As Long, _
  88.                                 ByVal picSource As StdPicture, _
  89.                                 ByVal xSrc As Long, _
  90.                                 ByVal ySrc As Long, _
  91.                                 Optional ByVal clrMask As OLE_COLOR = vbWhite, _
  92.                                 Optional ByVal clrHighlight As OLE_COLOR = vb3DHighlight, _
  93.                                 Optional ByVal clrShadow As OLE_COLOR = vb3DShadow, _
  94.                                 Optional ByVal hPal As Long = 0)
  95. Attribute PaintDisabledStdPic.VB_Description = "Paints a disabled appearing image (embossed) given a source picture object."
  96.     Dim hdcSrc As Long         'HDC that the source bitmap is selected into
  97.     Dim hbmMemSrcOld As Long
  98.     Dim hbmMemSrc As Long
  99.     Dim udtRect As RECT
  100.     Dim hbrMask As Long
  101.     Dim lMaskColor As Long
  102.     Dim hDcScreen As Long
  103.     Dim hPalOld As Long
  104.     
  105.     'Verify that the passed picture is not nothing
  106.     If picSource Is Nothing Then GoTo PaintDisabledDC_InvalidParam
  107.     Select Case picSource.Type
  108.         Case vbPicTypeBitmap
  109.             'Select passed picture into an HDC
  110.             hDcScreen = GetDC(0&)
  111.             'Validate palette
  112.             If hPal = 0 Then
  113.                 hPal = m_hpalHalftone
  114.             End If
  115.             hdcSrc = CreateCompatibleDC(hDcScreen)
  116.             hbmMemSrcOld = SelectObject(hdcSrc, picSource.handle)
  117.             hPalOld = SelectPalette(hdcSrc, hPal, True)
  118.             RealizePalette hdcSrc
  119.             
  120.             'Draw the bitmap
  121.             PaintDisabledDC hdcDest, xDest, yDest, Width, Height, hdcSrc, xSrc, ySrc, clrMask, clrHighlight, clrShadow, hPal
  122.             
  123.             SelectObject hdcSrc, hbmMemSrcOld
  124.             SelectPalette hdcSrc, hPalOld, True
  125.             RealizePalette hdcSrc
  126.             DeleteDC hdcSrc
  127.             ReleaseDC 0&, hDcScreen
  128.         Case vbPicTypeIcon
  129.             'Create a bitmap and select it into a DC
  130.             hDcScreen = GetDC(0&)
  131.             'Validate palette
  132.             If hPal = 0 Then
  133.                 hPal = m_hpalHalftone
  134.             End If
  135.             hdcSrc = CreateCompatibleDC(hDcScreen)
  136.             hbmMemSrc = CreateCompatibleBitmap(hDcScreen, Width, Height)
  137.             hbmMemSrcOld = SelectObject(hdcSrc, hbmMemSrc)
  138.             hPalOld = SelectPalette(hdcSrc, hPal, True)
  139.             RealizePalette hdcSrc
  140.             'Draw Icon onto DC
  141.             udtRect.Bottom = Height
  142.             udtRect.Right = Width
  143.             OleTranslateColor clrMask, 0&, lMaskColor
  144.             SetBkColor hdcSrc, lMaskColor
  145.             hbrMask = CreateSolidBrush(lMaskColor)
  146.             FillRect hdcSrc, udtRect, hbrMask
  147.             DeleteObject hbrMask
  148.             'DrawIcon hdcSrc, 0, 0, picSource.handle
  149.             DrawIconEx hdcSrc, 0, 0, picSource.handle, Width, Height, 0&, 0&, DI_NORMAL
  150.             'Draw Disabled image
  151.             PaintDisabledDC hdcDest, xDest, yDest, Width, Height, hdcSrc, 0&, 0&, clrMask, clrHighlight, clrShadow, hPal
  152.             'Clean up
  153.             SelectPalette hdcSrc, hPalOld, True
  154.             RealizePalette hdcSrc
  155.             DeleteObject SelectObject(hdcSrc, hbmMemSrcOld)
  156.             DeleteDC hdcSrc
  157.             ReleaseDC 0&, hDcScreen
  158.         Case Else
  159.             GoTo PaintDisabledDC_InvalidParam
  160.     End Select
  161.     Exit Sub
  162. PaintDisabledDC_InvalidParam:
  163.     Error.Raise giINVALID_PICTURE
  164.     Exit Sub
  165. End Sub
  166.  
  167. '-------------------------------------------------------------------------
  168. 'Purpose:   Creates a disabled-appearing (grayed) bitmap, given any format of
  169. '           input bitmap.
  170. 'In:
  171. '   [hdcDest]
  172. '           Device context to paint the picture on
  173. '   [xDest]
  174. '           X coordinate of the upper left corner of the area that the
  175. '           picture is to be painted on. (in pixels)
  176. '   [yDest]
  177. '           Y coordinate of the upper left corner of the area that the
  178. '           picture is to be painted on. (in pixels)
  179. '   [Width]
  180. '           Width of picture area to paint in pixels.  Note: If this value
  181. '           is outrageous (i.e.: you passed a forms ScaleWidth in twips
  182. '           instead of the pictures' width in pixels), this procedure will
  183. '           attempt to create bitmaps that require outrageous
  184. '           amounts of memory.
  185. '   [Height]
  186. '           Height of picture area to paint in pixels.  Note: If this
  187. '           value is outrageous (i.e.: you passed a forms ScaleHeight in
  188. '           twips instead of the pictures' height in pixels), this
  189. '           procedure will attempt to create bitmaps that require
  190. '           outrageous amounts of memory.
  191. '   [hdcSrc]
  192. '           Device context that contains the source picture
  193. '   [xSrc]
  194. '           X coordinate of the upper left corner of the area in the picture
  195. '           to use as the source. (in pixels)
  196. '   [ySrc]
  197. '           Y coordinate of the upper left corner of the area in the picture
  198. '           to use as the source. (in pixels)
  199. '   [clrMask]
  200. '           Color of pixels to be masked out
  201. '   [clrHighlight]
  202. '           Color to be used as outline highlight
  203. '   [clrShadow]
  204. '           Color to be used as outline shadow
  205. '   [hPal]
  206. '           Handle of palette to select into the memory DC's used to create
  207. '           the painting effect.
  208. '           If not provided, a HalfTone palette is used.
  209. '-------------------------------------------------------------------------
  210. Public Sub PaintDisabledDC(ByVal hdcDest As Long, _
  211.                                 ByVal xDest As Long, _
  212.                                 ByVal yDest As Long, _
  213.                                 ByVal Width As Long, _
  214.                                 ByVal Height As Long, _
  215.                                 ByVal hdcSrc As Long, _
  216.                                 ByVal xSrc As Long, _
  217.                                 ByVal ySrc As Long, _
  218.                                 Optional ByVal clrMask As OLE_COLOR = vbWhite, _
  219.                                 Optional ByVal clrHighlight As OLE_COLOR = vb3DHighlight, _
  220.                                 Optional ByVal clrShadow As OLE_COLOR = vb3DShadow, _
  221.                                 Optional ByVal hPal As Long = 0)
  222. Attribute PaintDisabledDC.VB_Description = "Paints a disabled appearing image (embossed) given a source hDC."
  223.     Dim hDcScreen As Long
  224.     Dim hbmMonoSection As Long
  225.     Dim hbmMonoSectionSav As Long
  226.     Dim hdcMonoSection As Long
  227.     Dim hdcColor As Long
  228.     Dim hdcDisabled As Long
  229.     Dim hbmDisabledSav As Long
  230.     Dim lpbi As BITMAPINFO
  231.     Dim hbmMono As Long
  232.     Dim hdcMono As Long
  233.     Dim hbmMonoSav As Long
  234.     Dim lMaskColor As Long
  235.     Dim lMaskColorCompare As Long
  236.     Dim hdcMaskedSource As Long
  237.     Dim hbmMasked As Long
  238.     Dim hbmMaskedOld As Long
  239.     Dim hpalMaskedOld As Long
  240.     Dim hpalDisabledOld As Long
  241.     Dim hpalMonoOld As Long
  242.     Dim rgbBlack As RGBQUAD
  243.     Dim rgbWhite As RGBQUAD
  244.     Dim dwSys3dShadow As Long
  245.     Dim dwSys3dHighlight As Long
  246.     Dim pvBits As Long
  247.     Dim rgbnew(1) As RGBQUAD
  248.     Dim hbmDisabled As Long
  249.     Dim lMonoBkGrnd As Long
  250.     Dim lMonoBkGrndChoices(2) As Long
  251.     Dim lIndex As Long  'For ... Next index
  252.     Dim hbrWhite As Long
  253.     Dim udtRect As RECT
  254.     
  255.     'TODO: handle pictures with dark masks
  256.     If hPal = 0 Then
  257.         hPal = m_hpalHalftone
  258.     End If
  259.   ' Define some colors
  260.     OleTranslateColor clrShadow, hPal, dwSys3dShadow
  261.     OleTranslateColor clrHighlight, hPal, dwSys3dHighlight
  262.     
  263.     hDcScreen = GetDC(0&)
  264.     With rgbBlack
  265.         .rgbBlue = 0
  266.         .rgbGreen = 0
  267.         .rgbRed = 0
  268.         .rgbReserved = 0
  269.     End With
  270.     With rgbWhite
  271.         .rgbBlue = 255
  272.         .rgbGreen = 255
  273.         .rgbRed = 255
  274.         .rgbReserved = 255
  275.     End With
  276.  
  277.     ' The first step is to create a monochrome bitmap with two colors:
  278.     ' white where colors in the original are light, and black
  279.     ' where the original is dark.  We can't simply bitblt to a bitmap.
  280.     ' Instead, we create a monochrome (bichrome?) DIB section and bitblt
  281.     ' to that.  Windows will do the conversion automatically based on the
  282.     ' DIB section's palette.  (I.e. using a DIB section, Windows knows how
  283.     ' to map "light" colors and "dark" colors to white/black, respectively.
  284.     With lpbi.bmiHeader
  285.         .biSize = LenB(lpbi.bmiHeader)
  286.         .biWidth = Width
  287.         .biHeight = -Height
  288.         .biPlanes = 1
  289.         .biBitCount = 1         ' monochrome
  290.         .biCompression = BI_RGB
  291.         .biSizeImage = 0
  292.         .biXPelsPerMeter = 0
  293.         .biYPelsPerMeter = 0
  294.         .biClrUsed = 0          ' max colors used (2^1 = 2)
  295.         .biClrImportant = 0     ' all (both :-]) colors are important
  296.     End With
  297.     With lpbi
  298.         .bmiColors(0) = rgbBlack
  299.         .bmiColors(1) = rgbWhite
  300.     End With
  301.  
  302.     hbmMonoSection = CreateDIBSection(hDcScreen, lpbi, DIB_RGB_COLORS, pvBits, 0&, 0)
  303.     
  304.     hdcMonoSection = CreateCompatibleDC(hDcScreen)
  305.     hbmMonoSectionSav = SelectObject(hdcMonoSection, hbmMonoSection)
  306.     
  307.     'Bitblt to the Monochrome DIB section
  308.     'If a mask color is provided, create a new bitmap and copy the source
  309.     'to it transparently.  If we don't do this, a dark mask color will be
  310.     'turned into the outline part of the monochrome DIB section
  311.     'Convert mask color and white before comparing
  312.     'because the Mask color might be a system color that would be evaluated
  313.     'to white.
  314.     OleTranslateColor vbWhite, hPal, lMaskColorCompare
  315.     OleTranslateColor clrMask, hPal, lMaskColor
  316.     If lMaskColor = lMaskColorCompare Then
  317.         BitBlt hdcMonoSection, 0, 0, Width, Height, hdcSrc, xSrc, ySrc, vbSrcCopy
  318.     Else
  319.         hbmMasked = CreateCompatibleBitmap(hDcScreen, Width, Height)
  320.         hdcMaskedSource = CreateCompatibleDC(hDcScreen)
  321.         hbmMaskedOld = SelectObject(hdcMaskedSource, hbmMasked)
  322.         hpalMaskedOld = SelectPalette(hdcMaskedSource, hPal, True)
  323.         RealizePalette hdcMaskedSource
  324.         'Fill the bitmap with white
  325.         With udtRect
  326.             .Left = 0
  327.             .Top = 0
  328.             .Right = Width
  329.             .Bottom = Height
  330.         End With
  331.         hbrWhite = CreateSolidBrush(vbWhite)
  332.         FillRect hdcMaskedSource, udtRect, hbrWhite
  333.         DeleteObject hbrWhite
  334.         'Do the transparent paint
  335.         PaintTransparentDC hdcMaskedSource, 0, 0, Width, Height, hdcSrc, xSrc, ySrc, lMaskColor, hPal
  336.         'BitBlt to the Mono DIB section.  The mask color has been turned to white.
  337.         BitBlt hdcMonoSection, 0, 0, Width, Height, hdcMaskedSource, 0, 0, vbSrcCopy
  338.         'Clean up
  339.         SelectPalette hdcMaskedSource, hpalMaskedOld, True
  340.         RealizePalette hdcMaskedSource
  341.         DeleteObject SelectObject(hdcMaskedSource, hbmMaskedOld)
  342.         DeleteDC hdcMaskedSource
  343.     End If
  344.       
  345.     ' Okay, we've got our B&W DIB section.
  346.     ' Now that we have our monochrome bitmap, the final appearance that we
  347.     ' want is this:  First, think of the black portion of the monochrome
  348.     ' bitmap as our new version of the original bitmap.  We want to have a dark
  349.     ' gray version of this with a light version underneath it, shifted down and
  350.     ' to the right.  The light acts as a highlight, and it looks like the original
  351.     ' image is a gray inset.
  352.     
  353.     ' First, create a copy of the destination.  Draw the light gray transparently,
  354.     ' and then draw the dark gray transparently
  355.     
  356.     hbmDisabled = CreateCompatibleBitmap(hDcScreen, Width, Height)
  357.     
  358.     hdcDisabled = CreateCompatibleDC(hDcScreen)
  359.     hbmDisabledSav = SelectObject(hdcDisabled, hbmDisabled)
  360.     hpalDisabledOld = SelectPalette(hdcDisabled, hPal, True)
  361.     RealizePalette hdcDisabled
  362.     'We used to fill the background with gray, instead copy the
  363.     'destination to memory DC.  This will allow a disabled image
  364.     'to be drawn over a background image.
  365.     BitBlt hdcDisabled, 0, 0, Width, Height, hdcDest, xDest, yDest, vbSrcCopy
  366.     
  367.     'When painting the monochrome bitmaps transparently onto the background
  368.     'we need a background color that is not the light color of the dark color
  369.     'Provide three choices to ensure a unique color is picked.
  370.     OleTranslateColor vbBlack, hPal, lMonoBkGrndChoices(0)
  371.     OleTranslateColor vbRed, hPal, lMonoBkGrndChoices(1)
  372.     OleTranslateColor vbBlue, hPal, lMonoBkGrndChoices(2)
  373.     
  374.     'Pick a background color choice that doesn't match
  375.     'the shadow or highlight color
  376.     For lIndex = 0 To 2
  377.         If lMonoBkGrndChoices(lIndex) <> dwSys3dHighlight And _
  378.                 lMonoBkGrndChoices(lIndex) <> dwSys3dShadow Then
  379.             'This color can be used for a mask
  380.             lMonoBkGrnd = lMonoBkGrndChoices(lIndex)
  381.             Exit For
  382.         End If
  383.     Next
  384.  
  385.     ' Now paint a the light color shifted and transparent over the background
  386.     ' It is not necessary to change the DIB section's color table
  387.     ' to equal the highlight color and mask color.  In fact, setting
  388.     ' the color table to anything besides black and white causes unpredictable
  389.     ' results (seen in win95 with IE4, using 256 colors).
  390.     ' Setting the Back and Text colors of the Monochrome bitmap, ensure
  391.     ' that the desired colors are produced.
  392.     With rgbnew(0)
  393.         .rgbRed = (vbWhite \ 2 ^ 16) And &HFF
  394.         .rgbGreen = (vbWhite \ 2 ^ 8) And &HFF
  395.         .rgbBlue = vbWhite And &HFF
  396.     End With
  397.     With rgbnew(1)
  398.         .rgbRed = (vbBlack \ 2 ^ 16) And &HFF
  399.         .rgbGreen = (vbBlack \ 2 ^ 8) And &HFF
  400.         .rgbBlue = vbBlack And &HFF
  401.     End With
  402.         
  403.     SetDIBColorTable hdcMonoSection, 0, 2, rgbnew(0)
  404.     
  405.     '...We can't pass a DIBSection to PaintTransparentDC(), so we need to
  406.     ' make a copy of our mono DIBSection.  Notice that we only need a monochrome
  407.     ' bitmap, but we must set its back/fore colors to the monochrome colors we
  408.     ' want (light gray and black), and PaintTransparentDC() will honor them.
  409.     hbmMono = CreateBitmap(Width, Height, 1, 1, ByVal 0&)
  410.     hdcMono = CreateCompatibleDC(hDcScreen)
  411.     hbmMonoSav = SelectObject(hdcMono, hbmMono)
  412.     SetMapMode hdcMono, GetMapMode(hdcSrc)
  413.     SetBkColor hdcMono, dwSys3dHighlight
  414.     SetTextColor hdcMono, lMonoBkGrnd
  415.     hpalMonoOld = SelectPalette(hdcMono, hPal, True)
  416.     RealizePalette hdcMono
  417.     BitBlt hdcMono, 0, 0, Width, Height, hdcMonoSection, 0, 0, vbSrcCopy
  418.  
  419.     '...We can go ahead and call PaintTransparentDC with our monochrome
  420.     ' copy
  421.     ' Draw this transparently over the disabled bitmap
  422.     '...Don't forget to shift right and left....
  423.     PaintTransparentDC hdcDisabled, 1, 1, Width, Height, hdcMono, 0, 0, lMonoBkGrnd, hPal
  424.     
  425.     ' Now draw a transparent copy, using dark gray where the monochrome had
  426.     ' black, and transparent elsewhere.  We'll use a transparent color of black.
  427.  
  428.     '...We can't pass a DIBSection to PaintTransparentDC(), so we need to
  429.     ' make a copy of our mono DIBSection.  Notice that we only need a monochrome
  430.     ' bitmap, but we must set its back/fore colors to the monochrome colors we
  431.     ' want (dark gray and black), and PaintTransparentDC() will honor them.
  432.     ' Use hbmMono and hdcMono; already created for first color
  433.     SetBkColor hdcMono, dwSys3dShadow
  434.     SetTextColor hdcMono, lMonoBkGrnd
  435.     BitBlt hdcMono, 0, 0, Width, Height, hdcMonoSection, 0, 0, vbSrcCopy
  436.  
  437.     '...We can go ahead and call PaintTransparentDC with our monochrome
  438.     ' copy
  439.     ' Draw this transparently over the disabled bitmap
  440.     PaintTransparentDC hdcDisabled, 0, 0, Width, Height, hdcMono, 0, 0, lMonoBkGrnd, hPal
  441.     BitBlt hdcDest, xDest, yDest, Width, Height, hdcDisabled, 0, 0, vbSrcCopy
  442.     ' Okay, we're done!
  443.     SelectPalette hdcDisabled, hpalDisabledOld, True
  444.     RealizePalette hdcDisabled
  445.     DeleteObject SelectObject(hdcMonoSection, hbmMonoSectionSav)
  446.     DeleteDC hdcMonoSection
  447.     DeleteObject SelectObject(hdcDisabled, hbmDisabledSav)
  448.     DeleteDC hdcDisabled
  449.     DeleteObject SelectObject(hdcMono, hbmMonoSav)
  450.     SelectPalette hdcMono, hpalMonoOld, True
  451.     RealizePalette hdcMono
  452.     DeleteDC hdcMono
  453.     ReleaseDC 0&, hDcScreen
  454. End Sub
  455.  
  456. '-------------------------------------------------------------------------
  457. 'Purpose:   Draws a transparent bitmap to a DC.  The pixels of the passed
  458. '           bitmap that match the passed mask color will not be painted
  459. '           to the destination DC
  460. 'In:
  461. '   [hdcDest]
  462. '           Device context to paint the picture on
  463. '   [xDest]
  464. '           X coordinate of the upper left corner of the area that the
  465. '           picture is to be painted on. (in pixels)
  466. '   [yDest]
  467. '           Y coordinate of the upper left corner of the area that the
  468. '           picture is to be painted on. (in pixels)
  469. '   [Width]
  470. '           Width of picture area to paint in pixels.  Note: If this value
  471. '           is outrageous (i.e.: you passed a forms ScaleWidth in twips
  472. '           instead of the pictures' width in pixels), this procedure will
  473. '           attempt to create bitmaps that require outrageous
  474. '           amounts of memory.
  475. '   [Height]
  476. '           Height of picture area to paint in pixels.  Note: If this
  477. '           value is outrageous (i.e.: you passed a forms ScaleHeight in
  478. '           twips instead of the pictures' height in pixels), this
  479. '           procedure will attempt to create bitmaps that require
  480. '           outrageous amounts of memory.
  481. '   [hdcSrc]
  482. '           Device context that contains the source picture
  483. '   [xSrc]
  484. '           X coordinate of the upper left corner of the area in the picture
  485. '           to use as the source. (in pixels)
  486. '   [ySrc]
  487. '           Y coordinate of the upper left corner of the area in the picture
  488. '           to use as the source. (in pixels)
  489. '   [clrMask]
  490. '           Color of pixels to be masked out
  491. '   [hPal]
  492. '           Handle of palette to select into the memory DC's used to create
  493. '           the painting effect.
  494. '           If not provided, a HalfTone palette is used.
  495. '-------------------------------------------------------------------------
  496. Public Sub PaintTransparentDC(ByVal hdcDest As Long, _
  497.                                     ByVal xDest As Long, _
  498.                                     ByVal yDest As Long, _
  499.                                     ByVal Width As Long, _
  500.                                     ByVal Height As Long, _
  501.                                     ByVal hdcSrc As Long, _
  502.                                     ByVal xSrc As Long, _
  503.                                     ByVal ySrc As Long, _
  504.                                     ByVal clrMask As OLE_COLOR, _
  505.                                     Optional ByVal hPal As Long = 0)
  506. Attribute PaintTransparentDC.VB_Description = "Paints an image with transparent pixels defined by the mask color.  Accepts an hDC as its image source."
  507.     Dim hdcMask As Long        'HDC of the created mask image
  508.     Dim hdcColor As Long       'HDC of the created color image
  509.     Dim hbmMask As Long        'Bitmap handle to the mask image
  510.     Dim hbmColor As Long       'Bitmap handle to the color image
  511.     Dim hbmColorOld As Long
  512.     Dim hbmMaskOld As Long
  513.     Dim hPalOld As Long
  514.     Dim hDcScreen As Long
  515.     Dim hdcScnBuffer As Long         'Buffer to do all work on
  516.     Dim hbmScnBuffer As Long
  517.     Dim hbmScnBufferOld As Long
  518.     Dim hPalBufferOld As Long
  519.     Dim lMaskColor As Long
  520.     
  521.     hDcScreen = GetDC(0&)
  522.     'Validate palette
  523.     If hPal = 0 Then
  524.         hPal = m_hpalHalftone
  525.     End If
  526.     OleTranslateColor clrMask, hPal, lMaskColor
  527.     
  528.     'Create a color bitmap to server as a copy of the destination
  529.     'Do all work on this bitmap and then copy it back over the destination
  530.     'when it's done.
  531.     hbmScnBuffer = CreateCompatibleBitmap(hDcScreen, Width, Height)
  532.     'Create DC for screen buffer
  533.     hdcScnBuffer = CreateCompatibleDC(hDcScreen)
  534.     hbmScnBufferOld = SelectObject(hdcScnBuffer, hbmScnBuffer)
  535.     hPalBufferOld = SelectPalette(hdcScnBuffer, hPal, True)
  536.     RealizePalette hdcScnBuffer
  537.     'Copy the destination to the screen buffer
  538.     BitBlt hdcScnBuffer, 0, 0, Width, Height, hdcDest, xDest, yDest, vbSrcCopy
  539.     
  540.     'Create a (color) bitmap for the cover (can't use CompatibleBitmap with
  541.     'hdcSrc, because this will create a DIB section if the original bitmap
  542.     'is a DIB section)
  543.     hbmColor = CreateCompatibleBitmap(hDcScreen, Width, Height)
  544.     'Now create a monochrome bitmap for the mask
  545.     hbmMask = CreateBitmap(Width, Height, 1, 1, ByVal 0&)
  546.     'First, blt the source bitmap onto the cover.  We do this first
  547.     'and then use it instead of the source bitmap
  548.     'because the source bitmap may be
  549.     'a DIB section, which behaves differently than a bitmap.
  550.     '(Specifically, copying from a DIB section to a monochrome bitmap
  551.     'does a nearest-color selection rather than painting based on the
  552.     'backcolor and forecolor.
  553.     hdcColor = CreateCompatibleDC(hDcScreen)
  554.     hbmColorOld = SelectObject(hdcColor, hbmColor)
  555.     hPalOld = SelectPalette(hdcColor, hPal, True)
  556.     RealizePalette hdcColor
  557.     'In case hdcSrc contains a monochrome bitmap, we must set the destination
  558.     'foreground/background colors according to those currently set in hdcSrc
  559.     '(because Windows will associate these colors with the two monochrome colors)
  560.     SetBkColor hdcColor, GetBkColor(hdcSrc)
  561.     SetTextColor hdcColor, GetTextColor(hdcSrc)
  562.     BitBlt hdcColor, 0, 0, Width, Height, hdcSrc, xSrc, ySrc, vbSrcCopy
  563.     'Paint the mask.  What we want is white at the transparent color
  564.     'from the source, and black everywhere else.
  565.     hdcMask = CreateCompatibleDC(hDcScreen)
  566.     hbmMaskOld = SelectObject(hdcMask, hbmMask)
  567.  
  568.     'When bitblt'ing from color to monochrome, Windows sets to 1
  569.     'all pixels that match the background color of the source DC.  All
  570.     'other bits are set to 0.
  571.     SetBkColor hdcColor, lMaskColor
  572.     SetTextColor hdcColor, vbWhite
  573.     BitBlt hdcMask, 0, 0, Width, Height, hdcColor, 0, 0, vbSrcCopy
  574.     'Paint the rest of the cover bitmap.
  575.     '
  576.     'What we want here is black at the transparent color, and
  577.     'the original colors everywhere else.  To do this, we first
  578.     'paint the original onto the cover (which we already did), then we
  579.     'AND the inverse of the mask onto that using the DSna ternary raster
  580.     'operation (0x00220326 - see Win32 SDK reference, Appendix, "Raster
  581.     'Operation Codes", "Ternary Raster Operations", or search in MSDN
  582.     'for 00220326).  DSna [reverse polish] means "(not SRC) and DEST".
  583.     '
  584.     'When bitblt'ing from monochrome to color, Windows transforms all white
  585.     'bits (1) to the background color of the destination hdc.  All black (0)
  586.     'bits are transformed to the foreground color.
  587.     SetTextColor hdcColor, vbBlack
  588.     SetBkColor hdcColor, vbWhite
  589.     BitBlt hdcColor, 0, 0, Width, Height, hdcMask, 0, 0, DSna
  590.     'Paint the Mask to the Screen buffer
  591.     BitBlt hdcScnBuffer, 0, 0, Width, Height, hdcMask, 0, 0, vbSrcAnd
  592.     'Paint the Color to the Screen buffer
  593.     BitBlt hdcScnBuffer, 0, 0, Width, Height, hdcColor, 0, 0, vbSrcPaint
  594.     'Copy the screen buffer to the screen
  595.     BitBlt hdcDest, xDest, yDest, Width, Height, hdcScnBuffer, 0, 0, vbSrcCopy
  596.     'All done!
  597.     DeleteObject SelectObject(hdcColor, hbmColorOld)
  598.     SelectPalette hdcColor, hPalOld, True
  599.     RealizePalette hdcColor
  600.     DeleteDC hdcColor
  601.     DeleteObject SelectObject(hdcScnBuffer, hbmScnBufferOld)
  602.     SelectPalette hdcScnBuffer, hPalBufferOld, True
  603.     RealizePalette hdcScnBuffer
  604.     DeleteDC hdcScnBuffer
  605.     
  606.     DeleteObject SelectObject(hdcMask, hbmMaskOld)
  607.     DeleteDC hdcMask
  608.     ReleaseDC 0&, hDcScreen
  609. End Sub
  610.  
  611. '-------------------------------------------------------------------------
  612. 'Purpose:   Draws a transparent bitmap to a DC.  The pixels of the passed
  613. '           bitmap that match the passed mask color will not be painted
  614. '           to the destination DC
  615. 'In:
  616. '   [hdcDest]
  617. '           Device context to paint the picture on
  618. '   [xDest]
  619. '           X coordinate of the upper left corner of the area that the
  620. '           picture is to be painted on. (in pixels)
  621. '   [yDest]
  622. '           Y coordinate of the upper left corner of the area that the
  623. '           picture is to be painted on. (in pixels)
  624. '   [Width]
  625. '           Width of picture area to paint in pixels.  Note: If this value
  626. '           is outrageous (i.e.: you passed a forms ScaleWidth in twips
  627. '           instead of the pictures' width in pixels), this procedure will
  628. '           attempt to create bitmaps that require outrageous
  629. '           amounts of memory.
  630. '   [Height]
  631. '           Height of picture area to paint in pixels.  Note: If this
  632. '           value is outrageous (i.e.: you passed a forms ScaleHeight in
  633. '           twips instead of the pictures' height in pixels), this
  634. '           procedure will attempt to create bitmaps that require
  635. '           outrageous amounts of memory.
  636. '   [picSource]
  637. '           Standard Picture object to be used as the image source
  638. '   [xSrc]
  639. '           X coordinate of the upper left corner of the area in the picture
  640. '           to use as the source. (in pixels)
  641. '           Ignored if picSource is an Icon.
  642. '   [ySrc]
  643. '           Y coordinate of the upper left corner of the area in the picture
  644. '           to use as the source. (in pixels)
  645. '           Ignored if picSource is an Icon.
  646. '   [clrMask]
  647. '           Color of pixels to be masked out
  648. '   [hPal]
  649. '           Handle of palette to select into the memory DC's used to create
  650. '           the painting effect.
  651. '           If not provided, a HalfTone palette is used.
  652. '-------------------------------------------------------------------------
  653. Public Sub PaintTransparentStdPic(ByVal hdcDest As Long, _
  654.                                     ByVal xDest As Long, _
  655.                                     ByVal yDest As Long, _
  656.                                     ByVal Width As Long, _
  657.                                     ByVal Height As Long, _
  658.                                     ByVal picSource As Picture, _
  659.                                     ByVal xSrc As Long, _
  660.                                     ByVal ySrc As Long, _
  661.                                     ByVal clrMask As OLE_COLOR, _
  662.                                     Optional ByVal hPal As Long = 0)
  663. Attribute PaintTransparentStdPic.VB_Description = "Paints an image with transparent pixels defined by the mask color.  Accepts a picture object as its image source."
  664.     Dim hdcSrc As Long         'HDC that the source bitmap is selected into
  665.     Dim hbmMemSrcOld As Long
  666.     Dim hbmMemSrc As Long
  667.     Dim udtRect As RECT
  668.     Dim hbrMask As Long
  669.     Dim lMaskColor As Long
  670.     Dim hDcScreen As Long
  671.     Dim hPalOld As Long
  672.     'Verify that the passed picture is a Bitmap
  673.     If picSource Is Nothing Then GoTo PaintTransparentStdPic_InvalidParam
  674.     
  675.     Select Case picSource.Type
  676.         Case vbPicTypeBitmap
  677.             hDcScreen = GetDC(0&)
  678.             'Validate palette
  679.             If hPal = 0 Then
  680.                 hPal = m_hpalHalftone
  681.             End If
  682.             'Select passed picture into an HDC
  683.             hdcSrc = CreateCompatibleDC(hDcScreen)
  684.             hbmMemSrcOld = SelectObject(hdcSrc, picSource.handle)
  685.             hPalOld = SelectPalette(hdcSrc, hPal, True)
  686.             RealizePalette hdcSrc
  687.             'Draw the bitmap
  688.             PaintTransparentDC hdcDest, xDest, yDest, Width, Height, hdcSrc, xSrc, ySrc, clrMask, hPal
  689.             
  690.             SelectObject hdcSrc, hbmMemSrcOld
  691.             SelectPalette hdcSrc, hPalOld, True
  692.             RealizePalette hdcSrc
  693.             DeleteDC hdcSrc
  694.             ReleaseDC 0&, hDcScreen
  695.         Case vbPicTypeIcon
  696.             'Create a bitmap and select it into an DC
  697.             hDcScreen = GetDC(0&)
  698.             'Validate palette
  699.             If hPal = 0 Then
  700.                 hPal = m_hpalHalftone
  701.             End If
  702.             hdcSrc = CreateCompatibleDC(hDcScreen)
  703.             hbmMemSrc = CreateCompatibleBitmap(hDcScreen, Width, Height)
  704.             hbmMemSrcOld = SelectObject(hdcSrc, hbmMemSrc)
  705.             hPalOld = SelectPalette(hdcSrc, hPal, True)
  706.             RealizePalette hdcSrc
  707.             'Draw Icon onto DC
  708.             udtRect.Bottom = Height
  709.             udtRect.Right = Width
  710.             OleTranslateColor clrMask, 0&, lMaskColor
  711.             hbrMask = CreateSolidBrush(lMaskColor)
  712.             FillRect hdcSrc, udtRect, hbrMask
  713.             DeleteObject hbrMask
  714.             'DrawIcon hdcSrc, 0, 0, picSource.handle
  715.             DrawIconEx hdcSrc, 0, 0, picSource.handle, Width, Height, 0&, 0&, DI_NORMAL
  716.             'Draw Transparent image
  717.             PaintTransparentDC hdcDest, xDest, yDest, Width, Height, hdcSrc, 0, 0, lMaskColor, hPal
  718.             'Clean up
  719.             DeleteObject SelectObject(hdcSrc, hbmMemSrcOld)
  720.             SelectPalette hdcSrc, hPalOld, True
  721.             RealizePalette hdcSrc
  722.             DeleteDC hdcSrc
  723.             ReleaseDC 0&, hDcScreen
  724.         Case Else
  725.             GoTo PaintTransparentStdPic_InvalidParam
  726.     End Select
  727.     Exit Sub
  728. PaintTransparentStdPic_InvalidParam:
  729.     Err.Raise giINVALID_PICTURE
  730.     Exit Sub
  731. End Sub
  732.  
  733. '-------------------------------------------------------------------------
  734. 'Purpose:   Draws a standard picture object to a DC
  735. 'In:
  736. '   [hdcDest]
  737. '           Handle of the device context to paint the picture on
  738. '   [xDest]
  739. '           X coordinate of the upper left corner of the area that the
  740. '           picture is to be painted on. (in pixels)
  741. '   [yDest]
  742. '           Y coordinate of the upper left corner of the area that the
  743. '           picture is to be painted on. (in pixels)
  744. '   [Width]
  745. '           Width of picture area to paint in pixels.  Note: If this value
  746. '           is outrageous (i.e.: you passed a forms ScaleWidth in twips
  747. '           instead of the pictures' width in pixels), this procedure will
  748. '           attempt to create bitmaps that require outrageous
  749. '           amounts of memory.
  750. '   [Height]
  751. '           Height of picture area to paint in pixels.  Note: If this
  752. '           value is outrageous (i.e.: you passed a forms ScaleHeight in
  753. '           twips instead of the pictures' height in pixels), this
  754. '           procedure will attempt to create bitmaps that require
  755. '           outrageous amounts of memory.
  756. '   [picSource]
  757. '           Standard Picture object to be used as the image source
  758. '   [xSrc]
  759. '           X coordinate of the upper left corner of the area in the picture
  760. '           to use as the source. (in pixels)
  761. '           Ignored if picSource is an Icon.
  762. '   [ySrc]
  763. '           Y coordinate of the upper left corner of the area in the picture
  764. '           to use as the source. (in pixels)
  765. '           Ignored if picSource is an Icon.
  766. '   [hPal]
  767. '           Handle of palette to select into the memory DC's used to create
  768. '           the painting effect.
  769. '           If not provided, a HalfTone palette is used.
  770. '-------------------------------------------------------------------------
  771. Public Sub PaintNormalStdPic(ByVal hdcDest As Long, _
  772.                                     ByVal xDest As Long, _
  773.                                     ByVal yDest As Long, _
  774.                                     ByVal Width As Long, _
  775.                                     ByVal Height As Long, _
  776.                                     ByVal picSource As Picture, _
  777.                                     ByVal xSrc As Long, _
  778.                                     ByVal ySrc As Long, _
  779.                                     Optional ByVal hPal As Long = 0)
  780. Attribute PaintNormalStdPic.VB_Description = "Paints an image provided by a picture object to an hDC with no effects."
  781.     Dim hdcTemp As Long
  782.     Dim hPalOld As Long
  783.     Dim hbmMemSrcOld As Long
  784.     Dim hDcScreen As Long
  785.     Dim hbmMemSrc As Long
  786.     'Validate that a bitmap was passed in
  787.     If picSource Is Nothing Then GoTo PaintNormalStdPic_InvalidParam
  788.     Select Case picSource.Type
  789.         Case vbPicTypeBitmap
  790.             If hPal = 0 Then
  791.                 hPal = m_hpalHalftone
  792.             End If
  793.             hDcScreen = GetDC(0&)
  794.             'Create a DC to select bitmap into
  795.             hdcTemp = CreateCompatibleDC(hDcScreen)
  796.             hPalOld = SelectPalette(hdcTemp, hPal, True)
  797.             RealizePalette hdcTemp
  798.             'Select bitmap into DC
  799.             hbmMemSrcOld = SelectObject(hdcTemp, picSource.handle)
  800.             'Copy to destination DC
  801.             BitBlt hdcDest, xDest, yDest, Width, Height, hdcTemp, xSrc, ySrc, vbSrcCopy
  802.             'Cleanup
  803.             SelectObject hdcTemp, hbmMemSrcOld
  804.             SelectPalette hdcTemp, hPalOld, True
  805.             RealizePalette hdcTemp
  806.             DeleteDC hdcTemp
  807.             ReleaseDC 0&, hDcScreen
  808.         Case vbPicTypeIcon
  809.             'Create a bitmap and select it into an DC
  810.             'Draw Icon onto DC
  811.             DrawIconEx hdcDest, xDest, yDest, picSource.handle, Width, Height, 0&, 0&, DI_NORMAL
  812.         Case Else
  813.             GoTo PaintNormalStdPic_InvalidParam
  814.     End Select
  815.     Exit Sub
  816. PaintNormalStdPic_InvalidParam:
  817.     Err.Raise giINVALID_PICTURE
  818. End Sub
  819.  
  820. Private Sub Class_Initialize()
  821.     Dim hDcScreen As Long
  822.     'Create halftone palette
  823.     hDcScreen = GetDC(0&)
  824.     m_hpalHalftone = CreateHalftonePalette(hDcScreen)
  825.     ReleaseDC 0&, hDcScreen
  826. End Sub
  827.  
  828. Private Sub Class_Terminate()
  829.     DeleteObject m_hpalHalftone
  830. End Sub
  831.  
  832. Public Sub PaintTransCornerDC(ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long)
  833.    '
  834.    ' 32-Bit Transparent BitBlt Function
  835.    ' Written by Geoff Glaze 2/13/98
  836.    '
  837.    ' Purpose:
  838.    '    Creates a transparent bitmap using lower left pixel of source bitmap
  839.    '
  840.    ' Parameters ************************************************************
  841.    '   hDestDC:     Destination device context
  842.    '   x, y:        Upper-left destination coordinates (pixels)
  843.    '   nWidth:      Width of destination
  844.    '   nHeight:     Height of destination
  845.    '   hSrcDC:      Source device context
  846.    '   xSrc, ySrc:  Upper-left source coordinates (pixels)
  847.    ' ***********************************************************************
  848.    
  849.    Dim iBackColor As Long
  850.     
  851.    iBackColor = GetPixel(hSrcDC, 0, 0)
  852.    If iBackColor = CLR_INVALID Then
  853.         'invalid color (specified point is outside of the clipping region)
  854.         'use default grey (standard bitmap back color)
  855.         iBackColor = &HC0C0C0
  856.     End If
  857.    
  858.    PaintTransparentDC hDestDC, x, y, nWidth, nHeight, hSrcDC, xSrc, ySrc, iBackColor
  859.    
  860. End Sub
  861.  
  862. Public Sub PaintDisabledCornerDC(ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long)
  863.    '
  864.    ' 32-Bit Transparent BitBlt Function
  865.    ' Written by Geoff Glaze 2/13/98
  866.    '
  867.    ' Purpose:
  868.    '    Creates a transparent bitmap using lower left pixel of source bitmap
  869.    '
  870.    ' Parameters ************************************************************
  871.    '   hDestDC:     Destination device context
  872.    '   x, y:        Upper-left destination coordinates (pixels)
  873.    '   nWidth:      Width of destination
  874.    '   nHeight:     Height of destination
  875.    '   hSrcDC:      Source device context
  876.    '   xSrc, ySrc:  Upper-left source coordinates (pixels)
  877.    ' ***********************************************************************
  878.    
  879.    Dim iBackColor As Long
  880.     
  881.    iBackColor = GetPixel(hSrcDC, 0, 0)
  882.    If iBackColor = CLR_INVALID Then
  883.         'invalid color (specified point is outside of the clipping region)
  884.         'use default grey (standard bitmap back color)
  885.         iBackColor = &HC0C0C0
  886.     End If
  887.    
  888.    PaintDisabledDC hDestDC, x, y, nWidth, nHeight, hSrcDC, xSrc, ySrc, iBackColor
  889.    
  890. End Sub
  891.  
  892. Public Sub PaintTransCornerStdPic(ByVal hdcDest As Long, _
  893.                                     ByVal xDest As Long, _
  894.                                     ByVal yDest As Long, _
  895.                                     ByVal Width As Long, _
  896.                                     ByVal Height As Long, _
  897.                                     ByVal picSource As Picture, _
  898.                                     ByVal xSrc As Long, _
  899.                                     ByVal ySrc As Long, _
  900.                                     Optional ByVal hPal As Long = 0)
  901.    '
  902.    ' 32-Bit Transparent BitBlt Function
  903.    ' Written by Geoff Glaze 2/13/98
  904.    '
  905.    ' Purpose:
  906.    '    Creates a transparent bitmap using lower left pixel of source bitmap
  907.    '
  908.    ' Parameters ************************************************************
  909.    '   hDestDC:     Destination device context
  910.    '   x, y:        Upper-left destination coordinates (pixels)
  911.    '   nWidth:      Width of destination
  912.    '   nHeight:     Height of destination
  913.    '   hSrcDC:      Source device context
  914.    '   xSrc, ySrc:  Upper-left source coordinates (pixels)
  915.    ' ***********************************************************************
  916.    
  917.     Dim hdcSrc As Long         'HDC that the source bitmap is selected into
  918.     Dim hbmMemSrcOld As Long
  919.     Dim hbmMemSrc As Long
  920.     Dim udtRect As RECT
  921.     Dim hbrMask As Long
  922.     Dim lMaskColor As Long
  923.     Dim hDcScreen As Long
  924.     Dim hPalOld As Long
  925.     'Verify that the passed picture is a Bitmap
  926.     If picSource Is Nothing Then GoTo PaintTransCornerStdPic_InvalidParam
  927.     
  928.     Select Case picSource.Type
  929.         Case vbPicTypeBitmap
  930.             hDcScreen = GetDC(0&)
  931.             'Validate palette
  932.             If hPal = 0 Then
  933.                 hPal = m_hpalHalftone
  934.             End If
  935.             'Select passed picture into an HDC
  936.             hdcSrc = CreateCompatibleDC(hDcScreen)
  937.             hbmMemSrcOld = SelectObject(hdcSrc, picSource.handle)
  938.             hPalOld = SelectPalette(hdcSrc, hPal, True)
  939.             RealizePalette hdcSrc
  940.             
  941.             'get back color
  942.             lMaskColor = GetPixel(hdcSrc, 0, 0)
  943.             If lMaskColor = CLR_INVALID Then
  944.                  'invalid color (specified point is outside of the clipping region)
  945.                  'use default grey (standard bitmap back color)
  946.                  lMaskColor = &HC0C0C0
  947.             End If
  948.             
  949.             'Draw the bitmap
  950.             PaintTransparentDC hdcDest, xDest, yDest, Width, Height, hdcSrc, xSrc, ySrc, lMaskColor, hPal
  951.             
  952.             SelectObject hdcSrc, hbmMemSrcOld
  953.             SelectPalette hdcSrc, hPalOld, True
  954.             RealizePalette hdcSrc
  955.             DeleteDC hdcSrc
  956.             ReleaseDC 0&, hDcScreen
  957.         Case vbPicTypeIcon
  958. '            'Create a bitmap and select it into an DC
  959. '            hDcScreen = GetDC(0&)
  960. '            'Validate palette
  961. '            If hPal = 0 Then
  962. '                hPal = m_hpalHalftone
  963. '            End If
  964. '            hdcSrc = CreateCompatibleDC(hDcScreen)
  965. '            hbmMemSrc = CreateCompatibleBitmap(hDcScreen, Width, Height)
  966. '            hbmMemSrcOld = SelectObject(hdcSrc, hbmMemSrc)
  967. '            hPalOld = SelectPalette(hdcSrc, hPal, True)
  968. '            RealizePalette hdcSrc
  969. '            'Draw Icon onto DC
  970. '            udtRect.Bottom = Height
  971. '            udtRect.Right = Width
  972. '
  973. '            'get back color
  974. '            lMaskColor = GetPixel(hdcSrc, 0, 0)
  975. '            If lMaskColor = CLR_INVALID Then
  976. '                 'invalid color (specified point is outside of the clipping region)
  977. '                 'use default grey (standard bitmap back color)
  978. '                 lMaskColor = &HC0C0C0
  979. '            End If
  980. '
  981. ''            OleTranslateColor clrMask, 0&, lMaskColor
  982. '            hbrMask = CreateSolidBrush(lMaskColor)
  983. '            FillRect hdcSrc, udtRect, hbrMask
  984. '            DeleteObject hbrMask
  985. '            DrawIcon hdcSrc, 0, 0, picSource.handle
  986. '            'Draw Transparent image
  987. '            PaintTransparentDC hdcDest, xDest, yDest, Width, Height, hdcSrc, 0, 0, lMaskColor, hPal
  988. '            'Clean up
  989. '            DeleteObject SelectObject(hdcSrc, hbmMemSrcOld)
  990. '            SelectPalette hdcSrc, hPalOld, True
  991. '            RealizePalette hdcSrc
  992. '            DeleteDC hdcSrc
  993. '            ReleaseDC 0&, hDcScreen
  994.         
  995.             'Create a bitmap and select it into an DC
  996.             'Draw Icon onto DC
  997.             DrawIconEx hdcDest, xDest, yDest, picSource.handle, Width, Height, 0&, 0&, DI_NORMAL
  998.         Case Else
  999.             GoTo PaintTransCornerStdPic_InvalidParam
  1000.     End Select
  1001.     Exit Sub
  1002.  
  1003. PaintTransCornerStdPic_InvalidParam:
  1004.     Err.Raise giINVALID_PICTURE
  1005.     Exit Sub
  1006.    
  1007. End Sub
  1008.  
  1009. Public Sub PaintDisabledCornerStdPic(ByVal hdcDest As Long, _
  1010.                                     ByVal xDest As Long, _
  1011.                                     ByVal yDest As Long, _
  1012.                                     ByVal Width As Long, _
  1013.                                     ByVal Height As Long, _
  1014.                                     ByVal picSource As Picture, _
  1015.                                     ByVal xSrc As Long, _
  1016.                                     ByVal ySrc As Long, _
  1017.                                     Optional ByVal hPal As Long = 0)
  1018.    '
  1019.    ' 32-Bit Transparent BitBlt Function
  1020.    ' Written by Geoff Glaze 2/13/98
  1021.    '
  1022.    ' Purpose:
  1023.    '    Creates a transparent bitmap using lower left pixel of source bitmap
  1024.    '
  1025.    ' Parameters ************************************************************
  1026.    '   hDestDC:     Destination device context
  1027.    '   x, y:        Upper-left destination coordinates (pixels)
  1028.    '   nWidth:      Width of destination
  1029.    '   nHeight:     Height of destination
  1030.    '   hSrcDC:      Source device context
  1031.    '   xSrc, ySrc:  Upper-left source coordinates (pixels)
  1032.    ' ***********************************************************************
  1033.    
  1034.     Dim hdcSrc As Long         'HDC that the source bitmap is selected into
  1035.     Dim hbmMemSrcOld As Long
  1036.     Dim hbmMemSrc As Long
  1037.     Dim udtRect As RECT
  1038.     Dim hbrMask As Long
  1039.     Dim lMaskColor As Long
  1040.     Dim hDcScreen As Long
  1041.     Dim hPalOld As Long
  1042.     'Verify that the passed picture is a Bitmap
  1043.     If picSource Is Nothing Then GoTo PaintDisabledCornerStdPic_InvalidParam
  1044.     
  1045.     Select Case picSource.Type
  1046.         Case vbPicTypeBitmap
  1047.             hDcScreen = GetDC(0&)
  1048.             'Validate palette
  1049.             If hPal = 0 Then
  1050.                 hPal = m_hpalHalftone
  1051.             End If
  1052.             'Select passed picture into an HDC
  1053.             hdcSrc = CreateCompatibleDC(hDcScreen)
  1054.             hbmMemSrcOld = SelectObject(hdcSrc, picSource.handle)
  1055.             hPalOld = SelectPalette(hdcSrc, hPal, True)
  1056.             RealizePalette hdcSrc
  1057.             
  1058.             'get back color
  1059.             lMaskColor = GetPixel(hdcSrc, 0, 0)
  1060.             If lMaskColor = CLR_INVALID Then
  1061.                  'invalid color (specified point is outside of the clipping region)
  1062.                  'use default grey (standard bitmap back color)
  1063.                  lMaskColor = &HC0C0C0
  1064.             End If
  1065.             
  1066.             'Draw the bitmap
  1067.             PaintDisabledDC hdcDest, xDest, yDest, Width, Height, hdcSrc, xSrc, ySrc, lMaskColor, , , hPal
  1068.             
  1069.             SelectObject hdcSrc, hbmMemSrcOld
  1070.             SelectPalette hdcSrc, hPalOld, True
  1071.             RealizePalette hdcSrc
  1072.             DeleteDC hdcSrc
  1073.             ReleaseDC 0&, hDcScreen
  1074.         Case vbPicTypeIcon
  1075. '            'Create a bitmap and select it into an DC
  1076. '            hDcScreen = GetDC(0&)
  1077. '            'Validate palette
  1078. '            If hPal = 0 Then
  1079. '                hPal = m_hpalHalftone
  1080. '            End If
  1081. '            hdcSrc = CreateCompatibleDC(hDcScreen)
  1082. '            hbmMemSrc = CreateCompatibleBitmap(hDcScreen, Width, Height)
  1083. '            hbmMemSrcOld = SelectObject(hdcSrc, hbmMemSrc)
  1084. '            hPalOld = SelectPalette(hdcSrc, hPal, True)
  1085. '            RealizePalette hdcSrc
  1086. '            'Draw Icon onto DC
  1087. '            udtRect.Bottom = Height
  1088. '            udtRect.Right = Width
  1089. ''            OleTranslateColor clrMask, 0&, lMaskColor
  1090. '
  1091. '            'get back color
  1092. '            lMaskColor = GetPixel(hdcSrc, 0, 0)
  1093. '            If lMaskColor = CLR_INVALID Then
  1094. '                 'invalid color (specified point is outside of the clipping region)
  1095. '                 'use default grey (standard bitmap back color)
  1096. '                 lMaskColor = &HC0C0C0
  1097. '            End If
  1098. '
  1099. '            hbrMask = CreateSolidBrush(lMaskColor)
  1100. '            FillRect hdcSrc, udtRect, hbrMask
  1101. '            DeleteObject hbrMask
  1102. '            DrawIcon hdcSrc, 0, 0, picSource.handle
  1103. '            'Draw Transparent image
  1104. '            PaintDisabledDC hdcDest, xDest, yDest, Width, Height, hdcSrc, 0, 0, lMaskColor, , , hPal
  1105. '            'Clean up
  1106. '            DeleteObject SelectObject(hdcSrc, hbmMemSrcOld)
  1107. '            SelectPalette hdcSrc, hPalOld, True
  1108. '            RealizePalette hdcSrc
  1109. '            DeleteDC hdcSrc
  1110. '            ReleaseDC 0&, hDcScreen
  1111.  
  1112.             'Create a bitmap and select it into an DC
  1113.             'Draw Icon onto DC
  1114.             DrawIconEx hdcDest, xDest, yDest, picSource.handle, Width, Height, 0&, 0&, DI_NORMAL
  1115.         Case Else
  1116.             GoTo PaintDisabledCornerStdPic_InvalidParam
  1117.     End Select
  1118.     Exit Sub
  1119.  
  1120. PaintDisabledCornerStdPic_InvalidParam:
  1121.     Err.Raise giINVALID_PICTURE
  1122.     Exit Sub
  1123.    
  1124. End Sub
  1125.  
  1126. Public Sub PaintGreyScaleCornerStdPic(ByVal hdcDest As Long, _
  1127.                                     ByVal xDest As Long, _
  1128.                                     ByVal yDest As Long, _
  1129.                                     ByVal Width As Long, _
  1130.                                     ByVal Height As Long, _
  1131.                                     ByVal picSource As Picture, _
  1132.                                     ByVal xSrc As Long, _
  1133.                                     ByVal ySrc As Long, _
  1134.                                     Optional ByVal hPal As Long = 0)
  1135.    '
  1136.    ' 32-Bit GreyScale BitBlt Function
  1137.    ' Written by Geoff Glaze 2/13/98
  1138.    '
  1139.    ' Purpose:
  1140.    '    Creates a greyscale version of a bitmap
  1141.    '
  1142.    ' Parameters ************************************************************
  1143.    '   hDestDC:     Destination device context
  1144.    '   x, y:        Upper-left destination coordinates (pixels)
  1145.    '   nWidth:      Width of destination
  1146.    '   nHeight:     Height of destination
  1147.    '   hSrcDC:      Source device context
  1148.    '   xSrc, ySrc:  Upper-left source coordinates (pixels)
  1149.    ' ***********************************************************************
  1150.    
  1151.     Dim hdcSrc As Long         'HDC that the source bitmap is selected into
  1152.     Dim hbmMemSrcOld As Long
  1153.     Dim hbmMemSrc As Long
  1154.     Dim udtRect As RECT
  1155.     Dim hbrMask As Long
  1156.     Dim lMaskColor As Long
  1157.     Dim hDcScreen As Long
  1158.     Dim hPalOld As Long
  1159.     Dim hBrush As Long
  1160.     'Verify that the passed picture is a Bitmap
  1161.     If picSource Is Nothing Then GoTo PaintGreyScaleCornerStdPic_InvalidParam
  1162.     
  1163.     hBrush = CreateSolidBrush(vbButtonShadow)
  1164.     Select Case picSource.Type
  1165.         Case vbPicTypeBitmap
  1166.             Call DrawState(hdcDest, hBrush, 0&, picSource, 0&, xDest, yDest, Width, Height, DST_BITMAP Or DSS_MONO)
  1167.         Case vbPicTypeIcon
  1168.             Call DrawState(hdcDest, hBrush, 0&, picSource, 0&, xDest, yDest, Width, Height, DST_ICON Or DSS_MONO)
  1169.         Case Else
  1170.             GoTo PaintGreyScaleCornerStdPic_InvalidParam
  1171.     End Select
  1172.     Exit Sub
  1173.  
  1174. PaintGreyScaleCornerStdPic_InvalidParam:
  1175.     Err.Raise giINVALID_PICTURE
  1176.     Exit Sub
  1177.    
  1178. End Sub
  1179.  
  1180. '-------------------------------------------------------------------------
  1181. 'Purpose:   Draws a standard picture object to a DC in Greyscale
  1182. 'In:
  1183. '   [hdcDest]
  1184. '           Handle of the device context to paint the picture on
  1185. '   [xDest]
  1186. '           X coordinate of the upper left corner of the area that the
  1187. '           picture is to be painted on. (in pixels)
  1188. '   [yDest]
  1189. '           Y coordinate of the upper left corner of the area that the
  1190. '           picture is to be painted on. (in pixels)
  1191. '   [Width]
  1192. '           Width of picture area to paint in pixels.  Note: If this value
  1193. '           is outrageous (i.e.: you passed a forms ScaleWidth in twips
  1194. '           instead of the pictures' width in pixels), this procedure will
  1195. '           attempt to create bitmaps that require outrageous
  1196. '           amounts of memory.
  1197. '   [Height]
  1198. '           Height of picture area to paint in pixels.  Note: If this
  1199. '           value is outrageous (i.e.: you passed a forms ScaleHeight in
  1200. '           twips instead of the pictures' height in pixels), this
  1201. '           procedure will attempt to create bitmaps that require
  1202. '           outrageous amounts of memory.
  1203. '   [picSource]
  1204. '           Standard Picture object to be used as the image source
  1205. '   [xSrc]
  1206. '           X coordinate of the upper left corner of the area in the picture
  1207. '           to use as the source. (in pixels)
  1208. '           Ignored if picSource is an Icon.
  1209. '   [ySrc]
  1210. '           Y coordinate of the upper left corner of the area in the picture
  1211. '           to use as the source. (in pixels)
  1212. '           Ignored if picSource is an Icon.
  1213. '   [hPal]
  1214. '           Handle of palette to select into the memory DC's used to create
  1215. '           the painting effect.
  1216. '           If not provided, a HalfTone palette is used.
  1217. '-------------------------------------------------------------------------
  1218. Public Sub PaintGreyScaleStdPic(ByVal hdcDest As Long, _
  1219.                                     ByVal xDest As Long, _
  1220.                                     ByVal yDest As Long, _
  1221.                                     ByVal Width As Long, _
  1222.                                     ByVal Height As Long, _
  1223.                                     ByVal picSource As Picture, _
  1224.                                     ByVal xSrc As Long, _
  1225.                                     ByVal ySrc As Long, _
  1226.                                     Optional ByVal hPal As Long = 0)
  1227.     Dim hdcTemp As Long
  1228.     Dim hPalOld As Long
  1229.     Dim hbmMemSrcOld As Long
  1230.     Dim hDcScreen As Long
  1231.     Dim hbmMemSrc As Long
  1232.     'Validate that a bitmap was passed in
  1233.     If picSource Is Nothing Then GoTo PaintGreyScaleStdPic_InvalidParam
  1234.     Select Case picSource.Type
  1235.         Case vbPicTypeBitmap
  1236.             If hPal = 0 Then
  1237.                 hPal = m_hpalHalftone
  1238.             End If
  1239.             hDcScreen = GetDC(0&)
  1240.             'Create a DC to select bitmap into
  1241.             hdcTemp = CreateCompatibleDC(hDcScreen)
  1242.             hPalOld = SelectPalette(hdcTemp, hPal, True)
  1243.             RealizePalette hdcTemp
  1244.             'Select bitmap into DC
  1245.             hbmMemSrcOld = SelectObject(hdcTemp, picSource.handle)
  1246.             'Copy to destination DC
  1247.             BitBlt hdcDest, xDest, yDest, Width, Height, hdcTemp, xSrc, ySrc, vbSrcAnd
  1248.             'Cleanup
  1249.             SelectObject hdcTemp, hbmMemSrcOld
  1250.             SelectPalette hdcTemp, hPalOld, True
  1251.             RealizePalette hdcTemp
  1252.             DeleteDC hdcTemp
  1253.             ReleaseDC 0&, hDcScreen
  1254.         Case vbPicTypeIcon
  1255.             'Create a bitmap and select it into an DC
  1256.             'Draw Icon onto DC
  1257.             DrawIconEx hdcDest, xDest, yDest, picSource.handle, Width, Height, 0&, 0&, DI_NORMAL
  1258.         Case Else
  1259.             GoTo PaintGreyScaleStdPic_InvalidParam
  1260.     End Select
  1261.     Exit Sub
  1262. PaintGreyScaleStdPic_InvalidParam:
  1263.     'Err.Raise giINVALID_PICTURE
  1264. End Sub
  1265.  
  1266. Public Function GetRedAmount(ByVal iColor As Long) As Long
  1267.     GetRedAmount = iColor Mod 256
  1268. End Function
  1269.  
  1270. Public Function GetGreenAmount(ByVal iColor As Long) As Long
  1271.     GetGreenAmount = (iColor \ 256) Mod 256
  1272. End Function
  1273.  
  1274. Public Function GetBlueAmount(ByVal iColor As Long) As Long
  1275.     GetBlueAmount = (iColor \ 256 ^ 2) Mod 256
  1276. End Function
  1277.  
  1278. Public Function AverageColors(ByVal iColor1 As Long, iColor2 As Long) As Long
  1279.     Dim xRed As Long
  1280.     Dim xGreen As Long
  1281.     Dim xBlue As Long
  1282.     xRed = (GetRedAmount(iColor1) + GetRedAmount(iColor2)) \ 2
  1283.     xGreen = (GetGreenAmount(iColor1) + GetGreenAmount(iColor2)) \ 2
  1284.     xBlue = (GetBlueAmount(iColor1) + GetBlueAmount(iColor2)) \ 2
  1285.     AverageColors = RGB(xRed, xGreen, xBlue)
  1286. End Function
  1287.